package com.lijunyan.blackmusic.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.lijunyan.blackmusic.entity.UserInfo;
import com.lijunyan.blackmusic.util.MyApplication;

import java.util.ArrayList;

/*
 * path: RestaurantDemo
 * Author: 86133
 * Create: 2019/12/3 11:35
 * Changes (from 2019/12/3)
 * Desc:
 */
public class DBUserInfoLogin {

    /***************************用户注册记录开始*******************************/
    /**
     * 查询注册记录
     * @param context
     * @param userName
     * @return
     */
    public static Boolean queryUserInfo(Context context, String userName) {
        try {
            String[] columns = {"userName"};
            String where = "userName=" + "\'" + userName + "\'";
            DBOpenHelper boh = DBOpenHelper.getinstance(context);
            SQLiteDatabase db = boh.getReadableDatabase();
            Cursor cursor = db.query(DBOpenHelper.TABLE_USER_INFO_NAME, columns, where, null, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    cursor.close();
                    return true;
                } else {
                    cursor.close();
                    return false;
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 存入注册用户
     * @param context
     * @param currentTimeMillis 排序使用
     * @return
     */
    public static long insertUserInfo(Context context, String userName, String userPassword, String currentTimeMillis) {
        DBOpenHelper boh = DBOpenHelper.getinstance(context);
        SQLiteDatabase db = boh.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("userName", userName);
        cv.put("userPassword", userPassword);
        cv.put("hadRead", "1");
        cv.put("currentTimeMillis", currentTimeMillis);
        
        long i = 0;
        Boolean hadSave = queryUserInfo(context, userName);
        if(hadSave){
            i = db.update(DBOpenHelper.TABLE_USER_INFO_NAME, cv, " userName=? ", new String[]{userName});
        }else{
            try {
                i = db.insertOrThrow(DBOpenHelper.TABLE_USER_INFO_NAME, null, cv);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return i;
    }

    /**
     * 修改注册记录
     * @param context
     * @param currentTimeMillis 排序使用
     * @return
     */
    public static int updateUserInfo(Context context, String userName, String userPassword, String currentTimeMillis) {
        DBOpenHelper boh = DBOpenHelper.getinstance(context);
        SQLiteDatabase db = boh.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("userName", userName);
        cv.put("userPassword", userPassword);
        cv.put("hadRead", "1");
        cv.put("currentTimeMillis", currentTimeMillis);
        return db.update(DBOpenHelper.TABLE_USER_INFO_NAME, cv, " userName=? ", new String[]{userName});
    }

    /**
     * 获取所有的注册记录
     * @return
     */
    public static ArrayList<UserInfo> queryUserInfoList(String userName) {
        ArrayList<UserInfo> userInfoList = new ArrayList<UserInfo>();
        try{
            DBOpenHelper boh = DBOpenHelper.getinstance(MyApplication.getContext());
            SQLiteDatabase db = boh.getWritableDatabase();
            String where = "userName=" + "\'" + userName + "\'";
            Cursor cursor = db.query(DBOpenHelper.TABLE_USER_INFO_NAME, null, where, null, null, null, null);
            if (null != cursor && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
                    userInfo.setUserPassword(cursor.getString(cursor.getColumnIndex("userPassword")));
                    userInfo.setRead(cursor.getString(cursor.getColumnIndex("hadRead")));
                    userInfoList.add(userInfo);
                }
                cursor.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return userInfoList;
    }

    /**
     * 获取所有的注册记录
     * @return
     */
    public static ArrayList<UserInfo> queryUserInfoList() {
        DBOpenHelper boh = DBOpenHelper.getinstance(MyApplication.getContext());
        SQLiteDatabase db = boh.getWritableDatabase();
        String where = "hadRead=1 " + "order by currentTimeMillis desc";
        Cursor cursor = db.query(DBOpenHelper.TABLE_USER_INFO_NAME, null, where, null, null, null, null);
        ArrayList<UserInfo> userInfoList = new ArrayList<UserInfo>();
        if (null != cursor && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
                userInfo.setUserPassword(cursor.getString(cursor.getColumnIndex("userPassword")));
                userInfo.setRead(cursor.getString(cursor.getColumnIndex("hadRead")));
                userInfoList.add(userInfo);
            }
            cursor.close();
        }
        return userInfoList;
    }

    /**
     * @param userName
     * @description:删除注册记录的一条数据
     * @author:wangweiwei
     * @return:void
     */
    public static int delUserInfoItem(Context context, String userName) {
        DBOpenHelper dbOpenHelper = DBOpenHelper.getinstance(context);
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        int delete = 0;
        try {
            delete = db.delete(DBOpenHelper.TABLE_USER_INFO_NAME, "userName=" + userName, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return delete;
    }


    /**
     * @param userInfoList
     * @description:删除注册记录的多条数据
     * @author:wangweiwei
     * @return:void
     */
    public static int delUserInfoList(Context context, ArrayList<UserInfo> userInfoList) {
        int delete = 0;
        if(userInfoList!=null && userInfoList.size()>0){
            for (int i = 0; i < userInfoList.size(); i++) {
                UserInfo userInfo = userInfoList.get(i);
                String contentid = userInfo.getUserName();
                if(!TextUtils.isEmpty(contentid)){
                    int flag = delUserInfoItem(context, contentid);
                    if(flag>0){
                        delete++;
                    }
                }
            }
        }
        return delete;
    }

    /**
     * @return void
     * @throws
     * @Description: 清空注册记录
     * @date 2019-8-8
     */
    public static void clearUserInfo(Context context) {
        DBOpenHelper dbOpenHelper = DBOpenHelper.getinstance(context);
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.delete(DBOpenHelper.TABLE_USER_INFO_NAME, null, null);
    }

    /***************************用户注册记录结束*******************************/
}
